package com.maoyu.cloud.api.Controller;

import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/api")
public class FileUploadController {

    @Resource
    private ObsClient obsClient;

    /**
     * 上传文件
     *
     * @param file 文件
     * @return 文件路径
     * @throws IOException IO异常
     */
    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            PutObjectResult putObjectResult = obsClient.putObject("cldk-data", fileName, inputStream);
            return putObjectResult.getObjectUrl();
        } catch (IOException e) {
            // 处理异常
            return "fail";
        }
    }
}